﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp.Controls
{
    public partial class CategoryListNew : System.Web.UI.UserControl
    {
        private CategoryManager m_CategoryManager = new CategoryManager();

        private int m_ShowCount = 10;
        public int ShowCount
        {
            get { return m_ShowCount; }
            set { m_ShowCount = value; }
        }

        private string m_imageSrc = "~/imagesWeb/product_01.gif";
        public string imageSrc
        {
            get { return m_imageSrc; }
            set { m_imageSrc = value; }
        }

        private string m_Category;
        public string Category
        {
            get { return m_Category; }
            set { m_Category = value; }
        }

        private string m_CategoryId;
        public string CategoryId
        {
            get { return m_CategoryId; }
        }

        private string m_Icon = "~/images/catalog_01_icon.gif";

        public string Icon
        {
            get { return m_Icon; }
            set { m_Icon = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindCategory();
            }
        }

        private void BindCategory()
        {
            try
            {
                Category aCategory =
                this.m_CategoryManager.GetCategoryByName(this.m_Category);
                this.rptCategory.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(aCategory).OrderBy(theCategory => theCategory.Index).Take(this.m_ShowCount);
                this.rptCategory.DataBind();
                this.m_CategoryId = aCategory.CategoryId.ToString();
                HtmlImage image = null;
                HtmlImage icon = null;
                foreach (var theControl in rptCategory.Controls)
                {
                    if ((theControl is RepeaterItem) && ((theControl as RepeaterItem).ItemType == ListItemType.Header))
                    {
                        image = (theControl as RepeaterItem).FindControl("imgHead") as HtmlImage;
                    }
                    image.Src = imageSrc;
                    if ((theControl is RepeaterItem) && ((theControl as RepeaterItem).ItemType == ListItemType.Item || (theControl as RepeaterItem).ItemType == ListItemType.AlternatingItem))
                    {
                        icon = (theControl as RepeaterItem).FindControl("imgCon") as HtmlImage;
                        icon.Src = m_Icon;
                    }
                    
                }
            }
            catch
            { }
        }
    }
}